import 'package:demo/app/widgets/line_horizontal.dart';
import 'package:demo/theme/app_colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../model/repay_plan_model.dart';
import '../../theme/app_text_theme.dart';


class ApplicationScrollTablePage extends StatefulWidget {
  final List<RepayPlan>? list;
  const ApplicationScrollTablePage({Key? key, this.list}) : super(key: key);

  @override
  State<ApplicationScrollTablePage> createState() => _ApplicationScrollTablePageState();
}

class _ApplicationScrollTablePageState extends State<ApplicationScrollTablePage> {
  final double _leftWidth = 50.w; // 期数的宽度
  final double _cell1Width = 80.w; // 还款金额的宽度
  final double _cell2Width = 380.w; // 明细的宽度
  final double _cellHeight = 38.w;
  final ScrollController _titleController = ScrollController();
  final ScrollController _contentController = ScrollController();

  @override
  void initState() {
    super.initState();
    _titleController.addListener(_updateContent);
    _contentController.addListener(_updateTitle);
  }

  void _updateTitle() {
    if (_titleController.offset != _contentController.offset) {
      _titleController.jumpTo(_contentController.offset);
    }
  }

  void _updateContent() {
    if (_contentController.offset != _titleController.offset) {
      _contentController.jumpTo(_titleController.offset);
    }
  }

  @override
  void dispose() {
    _titleController.removeListener(_updateContent);
    _contentController.removeListener(_updateTitle);
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize : MainAxisSize.min,
      children: [
          Container(
            width: Get.width,
            padding: EdgeInsets.symmetric(vertical: 12.w),
            decoration: BoxDecoration(
              color: Color_white,
              borderRadius: BorderRadius.vertical(top: Radius.circular(8.w)),
            ),
            child: Center(child: Text('还款计划表',style: text_131732_16)),
          ),
          const LineHorizontal(),
          Row(
            children: [
              Container(
                decoration: getBoxDecoration(),
                width: _leftWidth+0.2,
                height: _cellHeight+0.2,
                alignment: Alignment.center,
                child: Text('期数', style: text_131732_14),
              ),
              Expanded(
                  child: Container(
                    width: _cell1Width + _cell2Width + 0.4,
                    child: SingleChildScrollView(
                      controller: _titleController,
                      scrollDirection: Axis.horizontal,
                      child: Row(
                        children: [
                          Container(
                            decoration: getBoxDecoration(),
                            alignment: Alignment.center,
                            width: _cell1Width+0.2,
                            height: _cellHeight+0.2,
                            child: Text('还款金额',style: text_131732_14),
                          ),
                          Container(
                            padding: EdgeInsets.symmetric(horizontal: 12.w),
                            decoration: getBoxDecoration(),
                            alignment: Alignment.centerLeft,
                            width: _cell2Width+0.2,
                            height: _cellHeight+0.2,
                            child: Text('明细', style: text_131732_14),
                          ),
                        ]
                      ),
                    ),
                  ))
            ],
          ),
          Expanded( // 这里不能删除哦
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Row(
                    children: [
                      SizedBox(
                        width: _leftWidth + 0.2,
                        child: ListView.builder(
                          shrinkWrap: true,
                          physics: const NeverScrollableScrollPhysics(),
                          itemBuilder: (context, index) {
                            return Container(
                                alignment: const Alignment(0, 0),
                                decoration: getBoxDecoration(),
                                width: _leftWidth+0.2,
                                height: _cellHeight+0.2,
                                child: Text('${widget.list?[index].qs}'));
                          },
                          itemCount: widget.list?.length,
                        ),
                      ),
                      Expanded(
                          child: SingleChildScrollView(
                            controller: _contentController,
                            scrollDirection: Axis.horizontal,
                            child: SizedBox(
                              width: _cell1Width + _cell2Width + 0.4,
                              child: ListView.builder(
                                itemCount: widget.list?.length,
                                shrinkWrap: true,
                                physics: const NeverScrollableScrollPhysics(),
                                itemBuilder: (context, index) {
                                  return Row(
                                    children: _buildRightContent(index),
                                  );
                                },
                              ),
                              // ),
                            ),
                          ))
                    ],
                  ),
                ],
              ),
            ),
          )
        ],
      );
  }

  _buildRightContent(int index) {
    List<Widget> list = [];
    list.add(Container(
      alignment: const Alignment(0, 0),
      decoration: getBoxDecoration(),
      width: _cell1Width+0.2,
      height: _cellHeight+0.2,
      child: Text('${widget.list?[index].money}', style: text_131732_14),
    ));
    list.add(Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      alignment: const Alignment(-1, 0),
      decoration: getBoxDecoration(),
      width: _cell2Width+0.2,
      height: _cellHeight+0.2,
      child: Text('${widget.list?[index].mark}', style: text_131732_14),
    ));
    return list;
  }

  BoxDecoration getBoxDecoration({double bottomWidth = 0.2,double rightWidth = 0.2}){
    return BoxDecoration(color: Colors.white, border: Border(bottom: BorderSide(color: Color_line, width: bottomWidth), right: BorderSide(color: Color_line, width: rightWidth)));
  }
}
